import type {
  ChangePasswordForm,
  Course,
  Department,
  Enrollment,
  Pagination,
  Student,
  Teacher,
  User
} from "@/types/api-types";
import request from "@/utils/request";
import type { AxiosResponse } from "axios";

export interface ApiResponse<T> {
  code: number;
  message: string;
  data: T;
  total?: number; // 可选的分页总数
}

// 院系管理API
export const departmentApi = {
  // 获取所有院系
  getPage: (page: Pagination) => request.get("/departments", { params: page }),
  // 根据ID获取院系
  getById: (id: number) => request.get(`/departments/${id}`),
  // 创建院系
  create: (data: Department) => request.post("/departments", data),
  // 更新院系
  update: (id: number, data: Department) =>
    request.put(`/departments/${id}`, data),
  // 删除院系
  delete: (id: number) => request.delete(`/departments/${id}`),
  // 获取院系学生数量
  getStudentCount: (id: number): Promise<AxiosResponse<any>> =>
    request.get(`/departments/count/${id}`)
};

// 学生管理API
export const studentApi = {
  /**
   * 获取学生列表（包含多条件查询）
   * @param params 查询参数（所有参数均为可选）
   * @param page 分页参数（默认第0页，每页10条）
   */
  getPage: (params?: Student, page?: Pagination) => {
    // 合并查询参数和分页参数
    const queryParams = {
      ...params,
      page: page?.page ?? 0,
      size: page?.size ?? 10,
      sort: page?.sort
    };

    return request.get("/students", { params: queryParams });
  },
  getById: (id: string) => request.get(`/students/${id}`),
  create: (student: Student) => request.post("/students", student),
  update: (id: string, student: Student) =>
    request.put(`/students/${id}`, student),
  delete: (id: string) => request.delete(`/students/${id}`)
};

// 教师管理API
export const teacherApi = {
  getPage: (page: Pagination) => request.get("/teachers", { params: page }),
  getById: (id: string) => request.get(`/teachers/${id}`),
  create: (teacher: Teacher) => request.post("/teachers", teacher),
  update: (id: string, teacher: Teacher) =>
    request.put(`/teachers/${id}`, teacher),
  delete: (id: string) => request.delete(`/teachers/${id}`)
};

// 课程管理API
export const courseApi = {
  getPage: (page: Pagination) => request.get("/courses", { params: page }),
  getById: (id: string) => request.get(`/courses/${id}`),
  // 根据学生id获取所有已选课程
  getSelectedCoursesByStudentId: (studentId: string) =>
    request.get(`/courses/getSelectedCourses/${studentId}`),
  create: (course: Course) => request.post("/courses", course),
  update: (id: string, course: Course) => request.put(`/courses/${id}`, course),
  delete: (id: string) => request.delete(`/courses/${id}`)
  // getByDepartment: (deptId) =>
  //   request.get(`/courses/department/${deptId}`),
  // getByType: (type: any) => request.get(`/courses/type/${type}`)
};

// 选课管理API
export const enrollmentApi = {
  getPage: (page: Pagination) => request.get("/enrollments", { params: page }),
  // enroll: (enroll: Enrollment) => request.post("/enrollments/enroll", enroll),
  
  // 选课
  selectCourse: (studentId: string, courseId: string) =>
    request.post(`/enrollments/selectCourse/${studentId}/${courseId}`),
  // withdraw: (studentId: string, classId: number) =>
  //   request.post("/enrollments/withdraw", { studentId, classId }),

  // 退课
  drop: (studentId: string, courseId: string) => request.post(`/enrollments/drop/${studentId}/${courseId}`),
  getByStudent: (studentId: string) =>
    request.get(`/enrollments/student/${studentId}`),
  getByClass: (classId: number) => request.get(`/enrollments/class/${classId}`),
  updateScore: (enroll: Enrollment) => request.put("/enrollments/score", enroll)
};

// 身份验证Api
export const authApi = {
  login: (user: User) => request.post("/auth/login", user),
  // register: (user: User) => request.post("/auth/register", user),
  // 注册学生
  registerStudent: (student: Student) => request.post("/auth/register/student", student),
  // 注册教师
  registerTeacher: (teacher: Teacher) => request.post("/auth/register/teacher", teacher),
  changePassword: (form: ChangePasswordForm) =>
    request.post("/auth/change-password", form)
};
